package com.dldata.devtools30.controller.api;

import com.dldata.devtools30.controller.DevBase30Controller;
import com.dldata.devtools30.entity.DevtApiLogEntity;
import com.dldata.devtools30.repository.api.DevtApiLogEntityDao;
import com.dldata.devtools30.utils.DateUtil;
import com.dldata.devtools30.utils.Public;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.*;

import static com.dldata.devtools30.utils.DateUtil.FORMATER_YYYY_MM_DD;

@Controller
@RequestMapping(value = "/DevtApiLogEntityController")
public class DevtApiLogEntityController extends DevBase30Controller {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    DevtApiLogEntityDao devtApiLogEntityDao;
    
    @RequestMapping(value = "/getList")
    @ResponseBody
    public Result getList(ModelMap model, @RequestParam Map<String, String> paramMap){
        String stdate = paramMap.get("stdate");
        String eddate = paramMap.get("eddate");
        String searchValue = paramMap.get("searchValue");
        Date stdatedate = !StringUtil.isEmptyOrLength0(stdate)?DateUtil.strToDate(stdate):new Date(System.currentTimeMillis()-86400000L*3);
        Date eddatedate = !StringUtil.isEmptyOrLength0(eddate)?DateUtil.strToDate(eddate):new Date();
        if(StringUtil.isEmptyOrLength0(searchValue)){
            List<DevtApiLogEntity> apiLogList
                    = devtApiLogEntityDao.getApiLogList(new java.sql.Date(stdatedate.getTime()), new java.sql.Date(eddatedate.getTime()));
    
            return Result.ok(apiLogList);
        }
        List<DevtApiLogEntity> apiLogList
                = devtApiLogEntityDao.getApiLogList(searchValue, new java.sql.Date(stdatedate.getTime()), new java.sql.Date(eddatedate.getTime()));
    
        return  Result.ok(apiLogList);
    }
    
    @RequestMapping(value = "/submitlogstoredays")
    @ResponseBody
    public Result submitlogstoredays(ModelMap model, @RequestParam Map<String, String> paramMap){
        String logstoredays = paramMap.get("logstoredays");
        Public.logstoredays=Integer.parseInt(logstoredays);
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,Public.logstoredays*-1);
        jdbcTemplate.update(" delete devt_api_log where cdate < ? ",new Object[]{new Timestamp(calendar.getTimeInMillis())});
        return  Result.ok();
    }
}
